/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.ArrayList;

/**
 *
 * @author sbavdhorst
 */
public class Circle {
    private ArrayList<Character> circle;
    private int size;
    
    public Circle(int size){
        this.size = size;
        circle = new ArrayList<>();
    }
    
    public Circle(ArrayList<Character> circle){
        this.size = circle.size();
        this.circle = circle;
    }
    
    public Circle(char[] circle){
        this.size = circle.length;
        ArrayList<Character> circ = new ArrayList<>();
        for(char c: circle)
            circ.add(c);
        this.circle = circ;
    }
    
    public ArrayList<Character> getCircle(){
        return circle;
    }
    
    //size if it is full
    public int fullSize(){
        return size;
    }
    
    //size at the moment
    public int size(){
        return circle.size();
    }
    
    public boolean contains(char c){
        return circle.contains(c);
    }
    
    public boolean isEmpty(){
        return circle.isEmpty();
    }
    
    private boolean full(){
        return size == circle.size();
    }
    
//    //check for double characters on the circle
//    private boolean doubles(char c){
//            for(int j = 0; j<size; j++){
//                if(circle[j] == c)
//                    return true;
//            }
//            return false;
//    }
    
    public boolean add(int position, char c){
        if(this.contains(c) || this.full() || circle.size() >= size)
            return false;
        else{
            circle.add(position, c);
            return true;
        }
    }
    
    public boolean add(char[] c){
        if(size != c.length || this.full())
            return false;
        else{
            ArrayList<Character> circ = new ArrayList<>();
            for(char ch: c)
                circ.add(ch);
            
            circle = circ;
            return true;
        }
    }
    
    public ArrayList<Character> getTaxaNotOnCircle(Quartet q){
        ArrayList<Character> taxa = new ArrayList<>();
        
        if(!this.contains(q.getPair1()[0]))
            taxa.add(q.getPair1()[0]);
        if(!this.contains(q.getPair1()[1]))
            taxa.add(q.getPair1()[1]);
        if(!this.contains(q.getPair2()[0]))
            taxa.add(q.getPair2()[0]);
        if(!this.contains(q.getPair2()[1]))
            taxa.add(q.getPair2()[1]);

        return taxa;
    }           
    
    public int position(char c){
        return circle.indexOf(c);
    }
    
    public void clear(){
        circle.clear();
    }
    
    @Override
    public String toString(){
        StringBuilder s = new StringBuilder();
        for(char c: circle){
            s.append(c);
        }
        return s.toString();
    }
    
    public ArrayList<ArrayList<Quartet>> getAllQuartets(){
        ArrayList<ArrayList<Quartet>> allQuartets = new ArrayList<>();
        char[] alphabet = DenseSetGenerator.generateAlphabet(size);
        for(int i = 0; i<alphabet.length-3; i++){   //abcde -> abcd - abce - abde - acde - bcde
            for(int j = i+1; j< alphabet.length-2;j++){
                for(int k = j+1; k<alphabet.length-1; k++){
                    for(int l = k+1; l<alphabet.length; l++){
                        Quartet q1 = new Quartet(new char[]{alphabet[i], alphabet[j]}, new char[]{alphabet[k], alphabet[l]});
                        Quartet q2 = new Quartet(new char[]{alphabet[i], alphabet[k]}, new char[]{alphabet[j], alphabet[l]});
                        Quartet q3 = new Quartet(new char[]{alphabet[i], alphabet[l]}, new char[]{alphabet[k], alphabet[j]});
                        ArrayList<Quartet> quartets = new ArrayList<>();
                        if(q1.isOnCircle(this))
                            quartets.add(q1);
                        if(q2.isOnCircle(this))
                            quartets.add(q2);
                        if(q3.isOnCircle(this))
                            quartets.add(q3);
                        if(quartets.size() != 2)
                            System.out.println("Something's wrong!");
                        allQuartets.add(quartets);
                    }
                }
            }
        }
        return allQuartets;
    }
    
    public static void main(String[] args){
        Circle circle= new Circle(8);
        circle.add(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'});
        ArrayList<ArrayList<Quartet>> quartets = circle.getAllQuartets();
        for(ArrayList<Quartet> qs: quartets){
            for(Quartet q: qs){
                System.out.println(q);
            }
        }
    }
}
